<?php

namespace Home\Logic;

use Think\Model\RelationModel;
use Think\Page;

/**
 * 分类逻辑定义
 * Class CatsLogic
 * @package Home\Logic
 */
class UsersLogic extends RelationModel {
    
    /**
     * 注册
     * @param $username  邮箱或手机
     * @param $password  密码
     * @param $password2 确认密码
     * @return array
     */
    public function reg($data) {
        $username = $data['username'];
        $password = $data['password'];
        $password2 = $data['password2'];
        $is_validated = 0;
        if (check_email($username)) {
            $is_validated = 1;
            $map['email_validated'] = 1;
            $map['nickname'] = $map['email'] = $username; //邮箱注册
        }

        if (check_mobile($username)) {
            $is_validated = 1;
            $map['mobile_validated'] = 1;
            $map['nickname'] = $map['mobile'] = $username; //手机注册
        }
        $tjr_user = M('users')->where("mobile='{$data['tjr_accname']}' OR email='{$data['tjr_accname']}' OR account ='{$data['tjr_accname']}'")->field('user_id')->find();
        if (!$tjr_user)
            return array('status' => -1, 'msg' => '推荐人不存在，请重新输入', 'result' => '');

        if ($is_validated != 1)
            return array('status' => -1, 'msg' => '请用手机号或邮箱注册', 'result' => '');

        if ($data['level'] == 0)
            return array('status' => -1, 'msg' => '请选择会员级别', 'result' => '');

        if ($data['province'] == '' || $data['province'] == 0)
            return array('status' => -1, 'msg' => '请选择所在省份', 'result' => '');

        if ($data['city'] == '' || $data['city'] == 0)
            return array('status' => -1, 'msg' => '请选择所在城市', 'result' => '');

        if ($data['district'] == '' || $data['district'] == 0)
            return array('status' => -1, 'msg' => '请选择所在区域', 'result' => '');

        if ($data['twon'] == '' || $data['twon'] == 0)
            return array('status' => -1, 'msg' => '请选择所在街道', 'result' => '');

        if (!$username || !$password)
            return array('status' => -1, 'msg' => '请输入用户名或密码', 'result' => '');

        //验证两次密码是否匹配
        if ($password2 != $password)
            return array('status' => -1, 'msg' => '两次输入密码不一致', 'result' => '');
        //验证两次密码是否匹配
        if ($data['secpwd'] != $data['secpwd2'])
            return array('status' => -1, 'msg' => '两次二级密码输入不一致', 'result' => '');
        //验证是否存在用户名
        if (get_user_info($username, 1) || get_user_info($username, 2))
            return array('status' => -1, 'msg' => '账号已存在', 'result' => '');
        
        $dzuser = M('users')->where(array('province'=>$data['province'],'city'=>$data['city'],'district'=>$data['district'],'twon'=>$data['twon']))->select();
        if($dzuser){
            return array('status' => -1, 'msg' => '此地址已有人注册', 'result' => '');
        }
        $add['password']=md5($password);
        $add['reg_time']=time();
        $add['user_name']=$map['account'];
        $add['user_rank']=1;
        $map['password'] = encrypt($password); //密码
        $map['secpwd'] = encrypt($data['secpwd']); //密码
        $map['reg_time'] = time(); //注册时间
        $map['level'] = $data['level']; //会员级别
        $map['province'] = $data['province']; //省份
        $map['city'] = $data['city']; //城市
        $map['district'] = $data['district']; //镇
        $map['twon'] = $data['twon']; //街道
        $map['first_leader'] = $tjr_user['user_id']; // 推荐人id
        // 如果找到他老爸还要找他爷爷他祖父等
        if ($map['first_leader']) {
            $first_leader = M('users')->where("user_id = {$map['first_leader']}")->field('first_leader,second_leader')->find();
            $map['second_leader'] = $first_leader['first_leader'];
            $map['third_leader'] = $first_leader['second_leader'];
        } else {
            $map['first_leader'] = 0;
        }

        // 成为分销商条件
        //$distribut_condition = tpCache('distribut.condition');
        //if($distribut_condition == 0)  // 直接成为分销商, 每个人都可以做分销
        $map['is_distribut'] = 1; // 默认每个人都可以成为分销商

        $user_id = M('users')->add($map);
        // $ecs_user = M('users','ecs_','mysql://jshop111:jshop222@localhost/jshopdata#utf8'); 
        $ecs_user = M('users','ecs_','mysql://jmaaaaaa:jmbbbbbb@localhost:3306/jmalldata#utf8');
        if (!$user_id)
            return array('status' => -1, 'msg' => '注册失败', 'result' => '');
        $pay_points = tpCache('basic.reg_integral'); // 会员注册赠送积分
        //$add['pay_points']=$pay_points;
        $ecs_user->add($add);
        if ($pay_points > 0)
            accountLog($user_id, 0,0, $pay_points, 0, 0, 0, '会员注册赠送'); // 记录日志流水
        M('account_total_log')->add(array('user_id' => $res)); // 增加一条会员总收入数据
        $user = M('users')->where("user_id = {$user_id}")->find();
        // 会员注册送优惠券
        $coupon = M('coupon')->where("send_end_time > " . time() . " and ((createnum - send_num) > 0 or createnum = 0) and type = 2")->select();
        if (!empty($coupon)) {
            foreach ($coupon as $key => $val) {
                M('coupon_list')->add(array('cid' => $val['id'], 'type' => $val['type'], 'uid' => $user_id, 'send_time' => time()));
                M('Coupon')->where("id = {$val['id']}")->setInc('send_num'); // 优惠券领取数量加一
            }
        }
        return array('status' => 1, 'msg' => '注册成功', 'result' => $user);
    }

  

}
